package com.titar.robot.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.titar.robot.R;

import androidx.annotation.NonNull;


/**
 * Created by window on 2017/6/15.
 */

public class ComfirmDialog extends CustomDialog implements View.OnClickListener {

    private TextView mTvContent;
    private TextView mBtnOK;
    private TextView mBtnCancel;
    private View.OnClickListener mPositiveClickListener;
    private View.OnClickListener mNegativeClickListener;
    private String mTitle;
    private String mContentTex;

    private TextView mTvTitle;


    public static ComfirmDialog show(Context context,
                                     String title,
                                     String content,
                                     View.OnClickListener positiveClickListener,
                                     View.OnClickListener negativeClickListener) {
        ComfirmDialog comfirmDialog = new ComfirmDialog(context);
        comfirmDialog.setTitle(title);
        comfirmDialog.setContent(content);
        comfirmDialog.setPositiveButton(positiveClickListener);
        comfirmDialog.setNegativeButton(negativeClickListener);
        comfirmDialog.show();
        return comfirmDialog;
    }


    public ComfirmDialog(@NonNull Context context) {
        super(context, R.layout.dialog_comfirm);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        setCanceledOnTouchOutside(false);
    }

    public ComfirmDialog enableSystemDialog() {
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        return this;
    }

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mBtnOK = (TextView) findViewById(R.id.btn_ok);
        mBtnCancel = (TextView) findViewById(R.id.btn_cancel);

        mTvTitle.setText(mTitle);
        mTvTitle.setVisibility(TextUtils.isEmpty(mTvTitle.getText()) ? View.GONE : View.VISIBLE);

        mTvContent.setText(mContentTex);

        mBtnOK.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);

    }

    public void setTitle(String title) {
        mTitle = title;
        if (mTvTitle != null) {
            mTvTitle.setText(mTitle);
        }
    }

    public void setContent(String text) {
        mContentTex = text;
        if (mTvContent != null) {
            mTvContent.setText(text);
        }
    }

    public  void setContentLeft(){
        if (mTvContent != null) {
            mTvContent.setGravity(Gravity.LEFT);
        }
    }

    public void setOkText(String text){
        if (mBtnOK!=null)mBtnOK.setText(text);
    }

    public void setPositiveButton(View.OnClickListener listener) {
        mPositiveClickListener = listener;
    }

    public void setNegativeButton(View.OnClickListener listener) {
        mNegativeClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                if (mPositiveClickListener != null) {
                    mPositiveClickListener.onClick(v);
                }
                break;
            case R.id.btn_cancel:
                if (mNegativeClickListener != null) {
                    mNegativeClickListener.onClick(v);
                }
                break;
        }
        dismiss();
    }
}
